package pl.wroc.uni.ii.puzzle.data;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.util.Log;

public class DatabaseAdapter {

	private static final String DATABASE_NAME = "puzzle.db";
	public static final String HIGH_SCORE_TABLE_NAME = "highScore";
	public static final String COLUMN_ID_NAME = "id";
	public static final String COLUMN_DATE_NAME = "date"; 
	public static final String COLUMN_SCORE_NAME = "score";
	public static final String COLUMN_NAME_NAME = "name";
	public static final String COLUMN_HARDSHIP_NAME = "hardship";
	public static final int COLUMN_ID_INDEX = 0;
	public static final int COLUMN_DATE_INDEX = 1; 
	public static final int COLUMN_SCORE_INDEX = 2;
	public static final int COLUMN_NAME_INDEX = 3;
	public static final int COLUMN_HARDSHIP_INDEX = 4;
	private static final int DATABASE_VERSION = 1;
	
	private DatabaseHelper databaseHelper;
	private SQLiteDatabase database;
	
	public DatabaseAdapter(Context context) {
		databaseHelper = new DatabaseHelper(context, DATABASE_NAME, null, DATABASE_VERSION);
	}
	
	public DatabaseAdapter open() throws SQLException {
		database = databaseHelper.getWritableDatabase();
		return this;
	}
	
	public void close() {
		database.close();
	}
	
	public long insertHighScore(Score score) {
		ContentValues cv = new ContentValues();
		cv.put(COLUMN_DATE_NAME, score.getDate().getTime());
		cv.put(COLUMN_HARDSHIP_NAME, score.getHardship());
		cv.put(COLUMN_NAME_NAME, score.getName());
		cv.put(COLUMN_SCORE_NAME, score.getScore());
		String nullColumnHack = null;
		return database.insert(HIGH_SCORE_TABLE_NAME, nullColumnHack, cv);
	}
	
	public List<Score> getHighScores() {
		List<Score> scores = new LinkedList<Score>();
		String[] columns = new String[] {COLUMN_ID_NAME, COLUMN_DATE_NAME, COLUMN_HARDSHIP_NAME, COLUMN_NAME_NAME, COLUMN_SCORE_NAME};
		
		Cursor cursor = database.query(HIGH_SCORE_TABLE_NAME, columns, null, null, null, null, null);
		if (cursor.moveToFirst()) {
			do {
				Score score = new Score();
				score.setId(cursor.getLong(COLUMN_ID_INDEX));
				score.setDate(new Date(cursor.getLong(COLUMN_DATE_INDEX)));
				score.setScore(cursor.getLong(COLUMN_SCORE_INDEX));
				score.setHardship(cursor.getString(COLUMN_HARDSHIP_INDEX));
				score.setName(cursor.getString(COLUMN_NAME_INDEX));
				scores.add(score);
			} while (cursor.moveToNext());
		}
		cursor.close();
		return scores;
	}
	
	private static class DatabaseHelper extends SQLiteOpenHelper {

		public DatabaseHelper(Context context, String name, CursorFactory factory, int version) {
			super(context, name, factory, version);
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			String createTableSql = 
				"CREATE TABLE " + HIGH_SCORE_TABLE_NAME + "("
				    + COLUMN_ID_NAME+ " INTEGER PRIMARY KEY" + ", "
				    + COLUMN_DATE_NAME + " INTEGER NOT NULL" + ", "
				    + COLUMN_SCORE_NAME + " INTEGER NOT NULL" + ", "
				    + COLUMN_HARDSHIP_NAME + " VARCHAR(7) NOT NULL" + ", "
				    + COLUMN_NAME_NAME + " VARCHAR(25) NOT NULL );";
			db.execSQL(createTableSql);
			
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			Log.w("HighscoreDatabaseAdapter", "Upgrading from version " + oldVersion + "to "
					+ newVersion + ", which will destroy all old data");

			db.execSQL("DROP TABLE IF EXISTS " + HIGH_SCORE_TABLE_NAME);
			onCreate(db);			
		}
		
	}
}
